package cn.com.yx.ms.service.impl;

import cn.com.yx.ms.application.web.ApiPager;
import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.TestModel;
import cn.com.yx.ms.domain.dbmodel.CmdRecord;
import cn.com.yx.ms.domain.mq.MqCommandType;
import cn.com.yx.ms.persistence.CmdRecordMapper;
import cn.com.yx.ms.persistence.TestMapper;
import cn.com.yx.ms.service.IExternalDataService;
import cn.com.yx.ms.service.ITestService;
import cn.yx.framework.configuration.BaseConstant;
import cn.yx.framework.util.HttpClientUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;

/**
 * ApphubServiceImpl
 */
@Service
@Transactional
public class ExternalDataServiceImpl implements IExternalDataService {

    @Resource
    private CmdRecordMapper cmdRecordMapper;

    // 保存socket会话用Map
    private static Map<String, CmdRecord> websocketMap = new HashMap<String, CmdRecord>();

    //时间比较用参数
    private static Long timeMillions = 0L;

    @Override
    public String getJson(String devid, MqCommandType command) {
        return HttpClientUtil.sendGet(BaseConstant.getBusApiConfig().getUrl(), "deviceid=" + devid + "&command=" + command.getIndex());
    }

    /**
     * 获取公交线路小车数据
     * @param devid 设备id
     */
    @Override
    public void setWebSocketBusData(String devid) {
//        CmdRecord cmdRecord = new CmdRecord();
//        cmdRecord.setCmdtime(new Date());
//        cmdRecord.setCmdcode("5013");
//        cmdRecord.setCmdname("公交动态小车获取");
//        cmdRecord.setCmdtype("response");
//        cmdRecord.setEquipmentno(devid);
//
//        if (websocketMap.isEmpty()) {
//            timeMillions = Calendar.getInstance().getTimeInMillis();
//            websocketMap.put(UUID.randomUUID().toString(), cmdRecord);
//
//        } else {
//            // 超过10分钟则将数据写入DB
//            if (Calendar.getInstance().getTimeInMillis() - timeMillions > 600000) {
//                List<CmdRecord> list = new ArrayList<CmdRecord>();
//
//                // 创建时间
//                Date date = new Date();
//
//                // 遍历map，生成对象list
//                for (String key : websocketMap.keySet()) {
//                    CmdRecord cr = websocketMap.get(key);
//                    cr.setGuid(key);
//                    cr.setCreatetime(date);
//                    list.add(cr);
//                }
//
//                // cmdRecord 插入操作
//                if (list.size() > 0) {
//                    cmdRecordMapper.insertSelectiveList(list);
//                }
//
//                websocketMap.clear();
//                timeMillions = Calendar.getInstance().getTimeInMillis();
//                websocketMap.put(UUID.randomUUID().toString(), cmdRecord);
//            } else {
//                // 不超过10分钟，写入map
//                websocketMap.put(UUID.randomUUID().toString(), cmdRecord);
//            }
//        }
    }
}